/** 请求封装 */
export class Request {

    /** 请求地址 */
    private url: string;

    /** 请求头 */
    private header: any = { "content-type": "application/json" };

    /** 请求方式 */
    private type: string = "get";

    /** 请求参数 */
    private data: any;

    /** 成功回调 */
    private success: any = function (e) { };

    /** 失败回调 */
    private error: any = function (e) { };

    /** 执行Start方法 */
    private TimeoutFunction: any;

    /**
     * 
     * @param url 请求地址
     * @param header 请求头
     * @param data 请参数
     */
    constructor(url: string, data: any = null, header: any = null) {

        let that = this;
        that.url = url;
        that.header = header || that.header;
        that.data = data;
        that.TimeoutFunction = setTimeout(() => that.Start, 100);
    }

    Get(): Request {
        this.type = "get";
        this.ExecTimeoutFunction();
        return this;
    }
    Post(): Request {
        this.type = "post";
        this.ExecTimeoutFunction();
        return this;
    }
    Put(): Request {
        this.type = "put";
        this.ExecTimeoutFunction();
        return this;
    }
    Delete(): Request {
        this.type = "delete";
        this.ExecTimeoutFunction();
        return this;
    }


    Then(res): Request {
        this.success = res;
        this.ExecTimeoutFunction();
        return this;
    }

    Catch(error): Request {
        this.error = error;
        this.ExecTimeoutFunction();
        return this;
    }

    private ExecTimeoutFunction() {
        let that = this;
        clearTimeout(that.TimeoutFunction);

        that.TimeoutFunction = setTimeout(function () {
            that.Start();
        }, 200);
    }

    private Start() {

        var xmlHttp = null;

        if (XMLHttpRequest) {
            xmlHttp = new XMLHttpRequest();
        } else {
            // xmlHttp = new ActiveXObject('Microsoft.XMLHTTP');
        };

var that = this;


        switch (this.type.toUpperCase()) {

            case 'POST':
            case 'PUT':

                xmlHttp.open(this.type.toUpperCase(), this.url, true);

                // 设置请求头
                for (const key in this.header) {
                    if (!!this.header[key] || this.header[key] === 0) {
                        xmlHttp.setRequestHeader(key, this.header[key]);
                    }
                }

                xmlHttp.send(JSON.stringify(this.data));

                break;

            case 'GET':
            case 'DELETE':

                // 请求参数
                var params = [];
                for (const key in this.data) {
                    if (!!this.data[key] || this.data[key] === 0) {
                        params.push(key + '=' + this.data[key]);
                    }
                };
                var postData = params.join('&');

                xmlHttp.open(this.type.toUpperCase(), this.url + '?' + postData, true);

                // 设置请求头
                for (const key in this.header) {
                    if (!!this.header[key] || this.header[key] === 0) {
                        xmlHttp.setRequestHeader(key, this.header[key]);
                    }
                }

                xmlHttp.send(null);

                break;

        }

        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4 && xmlHttp.status == 200) {
                that.success(JSON.parse(xmlHttp.responseText));
            } else if(xmlHttp.readyState == 4 && xmlHttp.status != 200) {
                that.error(xmlHttp.responseText);
            }
        };
    }

}